// WTM默认页面 Wtm buidin page

using System.Linq;
using System.Threading.Tasks;
using TiaoTaoHelper.Wtm.Extension;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;

namespace WalkingTec.Mvvm.Mvc.Admin.ViewModels.FrameworkRoleVMs
{
    public class FrameworkRoleVm : BaseCRUDVM<FrameworkRole>
    {

        public override DuplicatedInfo<FrameworkRole> SetDuplicatedCheck()
        {
            var rv = CreateFieldsInfo(SimpleField(x => x.RoleName));
            rv.AddGroup(SimpleField(x => x.RoleCode));
            return rv;
        }

        public override void DoAdd()
        {
            base.DoAdd();
            Wtm.RemoveCache();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            base.DoEdit(updateAllFields);
            Wtm.RemoveCache();
        }

        public override async Task DoDeleteAsync()
        {
            await using var tran = DC.BeginTransaction();
            try
            {
                await base.DoDeleteAsync();
                var ur = DC.Set<FrameworkUserRole>().Where(x => x.RoleCode == Entity.RoleCode);
                DC.Set<FrameworkUserRole>().RemoveRange(ur);
                await DC.SaveChangesAsync();
                await tran.CommitAsync();
                Wtm.RemoveCache();
            }
            catch
            {
                await tran.RollbackAsync();
            }
        }
    }
}
